﻿using AutoMapper;
using mozhi.smarterp.AuthorCenter.ApiMetadatas;
using mozhi.smarterp.AuthorCenter.Departments;
using mozhi.smarterp.AuthorCenter.Identities.Clients;
using mozhi.smarterp.AuthorCenter.OpenIddict;
using mozhi.smarterp.AuthorCenter.Permissions;
using mozhi.smarterp.AuthorCenter.Permissions.Items;
using mozhi.smarterp.AuthorCenter.Permissions.PermissionGrants;
using mozhi.smarterp.AuthorCenter.Users;
using mozhi.smarterp.AuthorCenter.Users.Permissions;
using Volo.Abp.Identity;

namespace mozhi.smarterp.AuthorCenter;

public class AuthorCenterApplicationAutoMapperProfile : Profile
{
    public AuthorCenterApplicationAutoMapperProfile()
    {
        /* You can configure your AutoMapper mapping configuration here.
         * Alternatively, you can split your mapping configurations
         * into multiple profile classes for a better organization. */
        CreateMap<IdentityUser , IdentityUserDto>().MapExtraProperties();
        CreateMap<IdentityRoleDto, IdentityRole>().MapExtraProperties();

        CreateMap<UserTenant, UserTenantDto>()
            .ForMember(des => des.Id, opt => opt.MapFrom(src => src.Id))
            .ForMember(des => des.TenantId, opt => opt.MapFrom(src => src.TenantId))
            .ForMember(des => des.UserId, opt => opt.MapFrom(src => src.UserId))
            .ForMember(des => des.TargetUserId, opt => opt.MapFrom(src => src.TargetUserId))
            .ForMember(des => des.TenantName, opt => opt.Ignore())
            .ForMember(des => des.UserName, opt => opt.Ignore());

        CreateMap<OpenIddictClientType, OpenIddictClientTypeDto>()
            .ForMember(des => des.Id, opt => opt.MapFrom(src => src.Id))
            .ForMember(des => des.ClientId, opt => opt.MapFrom(src => src.ClientId))
            .ForMember(des => des.ClientType, opt => opt.MapFrom(src => src.ClientType));

        CreateMap<PermissionGroup, PermissionGroupDto>()
            .ForMember(des => des.Id, opt => opt.MapFrom(src => src.Id))
            .ForMember(des => des.Code, opt => opt.MapFrom(src => src.Code))
            .ForMember(des => des.Name, opt => opt.MapFrom(src => src.Name))
            .ForMember(des => des.Type, opt => opt.MapFrom(src => src.Type))
            .ForMember(des => des.Items, opt => opt.MapFrom(src => src.Items));

        CreateMap<PermissionItem, PermissionItemDto>()
           .ForMember(des => des.Id, opt => opt.MapFrom(src => src.Id))
           .ForMember(des => des.Code, opt => opt.MapFrom(src => src.Code))
           .ForMember(des => des.Name, opt => opt.MapFrom(src => src.Name))
           .ForMember(des => des.ParentId, opt => opt.MapFrom(src => src.ParentId));

        CreateMap<ApiMetadata, ApiMetadataDto>()
          .ForMember(des => des.Id, opt => opt.MapFrom(src => src.Id))
          .ForMember(des => des.Path, opt => opt.MapFrom(src => src.Path))
          .ForMember(des => des.Method, opt => opt.MapFrom(src => src.Method))
          .ForMember(des => des.ApiScope, opt => opt.MapFrom(src => src.ApiScope));

        CreateMap<PermissionItemApiGrant, PermissionItemApiGrantDto>()
          .ForMember(des => des.Id, opt => opt.MapFrom(src => src.Id))
          .ForMember(des => des.PermissionItemId, opt => opt.MapFrom(src => src.PermissionItemId))
          .ForMember(des => des.ApiMetadataId, opt => opt.MapFrom(src => src.ApiMetadataId))
          .ForMember(des => des.PermissionName, opt => opt.Ignore())
          .ForMember(des => des.ApiMetadataMethod, opt => opt.Ignore())
          .ForMember(des => des.ApiMetadataPath, opt => opt.Ignore());

        CreateMap<Department, DepartmentDto>()
         .ForMember(des => des.Id, opt => opt.MapFrom(src => src.Id))
         .ForMember(des => des.Code, opt => opt.MapFrom(src => src.Code))
         .ForMember(des => des.Name, opt => opt.MapFrom(src => src.Name))
         .ForMember(des => des.ParentId, opt => opt.MapFrom(src => src.ParentId))
         .ForMember(des => des.Activated, opt => opt.MapFrom(src => src.Activated))
         .ForMember(des => des.ParentCode, opt => opt.Ignore())
         .ForMember(des => des.ParentName, opt => opt.Ignore());

        CreateMap<UserDataPermission, UserDataPermissionDto>()
         .ForMember(des => des.Id, opt => opt.MapFrom(src => src.Id))
         .ForMember(des => des.IsOwnCreated, opt => opt.MapFrom(src => src.IsOwnCreated))
         .ForMember(des => des.IsOwnDepartment, opt => opt.MapFrom(src => src.IsOwnDepartment))
         .ForMember(des => des.UserId, opt => opt.MapFrom(src => src.UserId));
    }
}
